var ns;
Vue.component('tree-item', {
    template: '#item-template',
    props: {
        item: {}
    },
    data: function () {
        return {
            isOpen: true,
            selecttypeid:'',
        }
    },
    computed: {
        isFolder: function () {
            return this.item.children &&
                this.item.children.length
        }
    },
    methods: {
        toggle: function (item) {
            if (this.isFolder) {
                this.isOpen = !this.isOpen
            } else {
                this.choose_type(item);
            }
        },
        choose_type: function (item) {
            var that = this;
            app.currentchosenlist.id = item.ID;
            that.selecttypeid=item.ID;
            app.currentchosenlist.text = item.text;
        },
    }
})
app = new Vue({
    el: '#app',
    data: {
        form: {
            keywords: ''
        },
        childlist: [],
        currentchosenlist: [],
    },
    methods: {
        do_search: function() {
            var that = this;
            that.get_data();
        },
        get_data: function() {
            var that = this;
            ns.post({
                keywords: that.form.keywords,
                action: 'getdepartmentsdata',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.currentchosenlist=data.departments;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });   
        },
        do_save:function(item){
            var that=this;
            var options={};
            options.id=that.currentchosenlist.id;
            options.ID=that.currentchosenlist.ID;
            options.text=that.currentchosenlist.text;
            options.TypeID=that.currentchosenlist.TypeID;
            options.children=that.currentchosenlist.children;
            api.sendEvent({
                name: 'dosavequalitycontrol',
                extra: {
                    item: options,
                }
            });
            setTimeout(function() {
                api.closeWin();
            }, 500);
        }, 
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.get_data(); 
};